package Underworld_Survival;

import java.util.*;

public abstract class Weapon extends Equipment {
    protected transient String weaponEffectId; // 使用UUID作为唯一来源标识
    protected transient Map<AttributeType,EffectValue> onEquip;//应用武器时的具体效果
    protected transient Map<AttributeType,EffectValue> reverse;//移除武器时的效果
    protected transient EffectManager manager;//记录武器效果，用于正确移除
    protected transient EquipmentEffect effect;//武器普通的加成

    public Weapon(String weaponEffectId, EffectManager manager) {
        this.weaponEffectId = weaponEffectId;
        this.manager = manager;}
    public Weapon(String weaponEffectId, EffectManager manager, EquipmentEffect effect){
        this.weaponEffectId = weaponEffectId;
        this.manager = manager;
        this.effect = effect;}
    public Weapon(String name, String introduction, String itemEffect, CurrencyType purchaseCurrency,
                  int priceOfPurchase, int priceOfSelling,
                  Map<AttributeType, EffectValue> onEquipEffect  // 直接传入初始化好的效果数据
                ){
        super(name,introduction,itemEffect,purchaseCurrency,priceOfPurchase,priceOfSelling);
        this.weaponEffectId = UUID.randomUUID().toString()+"武器";
        this.onEquip=new HashMap<>(onEquipEffect);// 复制传入的效果数据
        this.reverse=new HashMap<>();
        this.effect=new EquipmentEffect(
                role -> {equip(role);},
                role -> {unequip(role);}
        );
    }
    public void equip(Role role) {
        // 注册效果并自动绑定到装备实例
        this.manager=role.getEffectManager();
        String effectId = manager.applyEffect(weaponEffectId, onEquip);
        Map<AttributeType, Float> actualValues = manager.getEffectsById(effectId,role);
        role.modifyAttributes(actualValues,true);
    }
    public void unequip(Role role) {
        Map<AttributeType, Float> reverse = manager.removeEffects(weaponEffectId,role);
        role.modifyAttributes(reverse,false);
    }


    public String getWeaponEffectId() {return weaponEffectId;}
    public Map<AttributeType, EffectValue> getReverse() {return reverse;}
    public void setReverse(Map<AttributeType, EffectValue> reverse) {this.reverse = reverse;}
    public EquipmentEffect getEffect() {return effect;}
    public void setEffect(EquipmentEffect effect) {this.effect = effect;}
    public EffectManager getManager(){return manager;}
    public void setManager(EffectManager manager){this.manager=manager;}
    public Map<AttributeType,EffectValue> getOnEquip(){
        return onEquip;
    }

}

//<飞剑>flyingSword
class flyingSword extends Weapon{
    public flyingSword() {
        super("<飞剑>", "锋利的剑，刺向敌人造成不少的伤害",
                "力量+5", CurrencyType.GOLD, 100, 50,
                // 直接传入预定义的效果数据
                Map.of(AttributeType.BONUS_STR, new FixedValue(5f))
        );
        weaponEffectId="飞剑"+weaponEffectId;
    }

    @Override
    public String getItemId() { return "weapon_flyingSword"; }

}
//<重刀>heavyKnife
class heavyKnife extends Weapon{
    public heavyKnife() {
        super("<重刀>", "非常沉重的大刀，拥有非凡的力气才能拿起",
                "力量+10  速度-5", CurrencyType.GOLD, 160, 80,
                // 直接传入预定义的效果数据
                Map.of(AttributeType.BONUS_STR, new FixedValue(10f),
                        AttributeType.BONUS_EP,new FixedValue(-5f))
        );
        weaponEffectId="重刀"+weaponEffectId;
    }

    @Override
    public String getItemId() { return "weapon_heavyKnife"; }

}
//<钻石之剑>diamondSword
class diamondSword extends Weapon{
    public diamondSword() {
        super("<钻石之剑>", "异常锋利，曾是某位武林高手所使用的武器",
                "力量+15", CurrencyType.GOLD, 250, 130,
                // 直接传入预定义的效果数据
                Map.of(AttributeType.BONUS_STR, new FixedValue(15f))
        );
        weaponEffectId="钻石之剑"+weaponEffectId;
    }

    @Override
    public String getItemId() { return "weapon_diamondSword"; }

}
//<烈焰之剑>flameSword
class flameSword extends Weapon{
    public flameSword() {
        super("<烈焰之剑>", "拔剑烈焰起，亦炎之威，主宰战场！",
                "力量+20 一定几率让敌人【灼烧】", CurrencyType.DIAMOND, 110, 250,
                // 直接传入预定义的效果数据
                Map.of(AttributeType.BONUS_STR, new FixedValue(20f))
        );
        addSpecialEffect(new FlameEffect(1f));//烈焰之剑特殊效果，烈焰
        weaponEffectId="烈焰之剑"+weaponEffectId;
    }

    @Override
    public String getItemId() { return "weapon_flameSword"; }

}

